#include "EventAggregator.h"

EventAggregator::EventAggregator()
{
	tag = 0;
	//handlerIndex = 0;
}

void EventAggregator::addHandler(EventHandler *handler)
{
	// currently, only functions of one particular class can be called (T *inst). Figure out if can be changed to generic class type.
	//EDIT I just figured it out. Oh no I didn't.
	//SECOND EDIT. I actually did! Recipe for multi-class-type eventaggregator: Add another array, with the corresponding instance pointer (at the same index as the pointer to the member function)
	//then just set it in this addHandler function! I'm a GENIUS!
	
	//Serial.print("Added handler to ");
	//Serial.print(tag,DEC);
	//Serial.print(" at position ");
/*
	handlers[handlerIndex] = handler;
	handlerIndex++;*/

	if(!singleHandler)
	{
		Serial.println("Added handler");
		singleHandler = handler;
	}
}



void EventAggregator::removeHandler(EventHandler *handler)
{
	//kijk of handler in handlers array staat, zo niet: return, zo wel:
	EventHandler *currHandler = 0;
	EventHandler *foundHandler = 0;
	
	//int i;
	//int size = handlerIndex+1;
	//for(i=0;i<size;i++)
	//{
	//	if (handlers[i]==handler)
	//	{
	//		foundHandler = handler;
	//		break;
	//	}
	//}
	//if(!foundHandler) return;

	//verwijder handler van list, shift alle bovenliggende een naar beneden.
	//i++;
	//for (int j=i;j<size;j++)
	//{
	//	handlers[i-1] = handlers[i];
	//}
	//handlerIndex--;

	if (handler==singleHandler)
	{
		singleHandler = 0;
	}
}
void EventAggregator::execute()
{
  //  for(unsigned int i=0;i<handlerIndex;i++)
  //  {
		//handlers[i]->invoke();
		//Serial.println(i);
  //  }
	if(singleHandler)
	{
		Serial.print("INVOKE");
		//singleHandler->invoke();
	}
}